import trimesh


def detect_collision(gun=None, workmesh=None,clamp=None):
    collision_rigion = trimesh.collision.CollisionManager()
    if gun:
        collision_rigion.add_object(name="gun",mesh=gun)

    if workmesh:
        collision_rigion.add_object(name="workmesh",mesh=workmesh)

    if clamp:
        clamp_mesh=trimesh.load(clamp)
        collision_rigion.add_object(name="clamp", mesh=clamp_mesh)

    result,names = collision_rigion.in_collision_internal(return_names=True)
    if result == False:
        return 0
    elif ('gun','workmesh') in names and ('gun','clamp') not in names and ('workmesh','clamp') not in names:
        return 1
    elif ('gun','workmesh') not in names and ('gun','clamp') in names and ('workmesh','clamp') not in names:
        return 2
    elif ('gun','workmesh') not in names and ('gun','clamp') not in names and ('workmesh','clamp') in names:
        return 3
    elif ('gun','workmesh') in names and ('gun','clamp') in names and ('workmesh','clamp') not in names:
        return 4
    elif ('gun','workmesh') in names and ('gun','clamp') not in names and ('workmesh','clamp') in names:
        return 5
    elif ('gun','workmesh') not in names and ('gun','clamp') in names and ('workmesh','clamp') in names:
        return 6
    elif ('gun','workmesh') in names and ('gun','clamp') in names and ('workmesh','clamp') in names:
        return 7





# clamp=trimesh.load_mesh("new_clamp.stl")
# clamp.show()


# gun=trimesh.load_mesh("gun.stl")
# workmesh=trimesh.load_mesh("work.stl")
# clamp="new_clamp.stl"
#
# print(type(workmesh))
# # workmesh.show()
#
# print(detect_collision(gun,workmesh,clamp))